package org.alvin.xl.hlht.client;

import cn.hutool.json.JSON;
import com.alibaba.fastjson.JSONObject;
import org.alvin.mini_inject.annotations.MiniComponent;
import org.alvin.mini_inject.annotations.MiniInject;
import org.alvin.xl.hlht.utils.AESUtil;
import org.alvin.xl.hlht.utils.HmacMD5;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 互联互通模拟器
 */
@MiniComponent
public class HlhtClientSimulator {

    @MiniInject
    private HlhtConfigSimulator configSimulator;
    @MiniInject
    private HlhtClientDecodeSimulator decodeSimulator;

    private AtomicLong seqData = new AtomicLong();

    private String getSeq() {
        return String.format("%04d", seqData.incrementAndGet()) + "";
    }

    /**
     * 数据打包
     *
     * @param data
     * @return
     * @throws Exception
     */
    private JSONObject packData(String data) throws Exception {
        try {
            String operator = this.configSimulator.getClientoperatorId();
            String seq = getSeq();
            String timeStamp = System.currentTimeMillis() + "";

            //加密
            System.out.println("加密前： URL=" + this.configSimulator.getServerpushUrl() + "  param = " + data);
            String sendData = AESUtil.encrypt(data, this.configSimulator.getServerdataKey(), this.configSimulator.getServerdataIv());
            System.out.println("加密后： URL=" + this.configSimulator.getServerpushUrl() + "  param = " + sendData);

            //签名
            String md = operator + sendData + timeStamp + seq;
            System.out.println("签名数据：" + md);
            System.out.println("签名key:" + this.configSimulator.getServersignKey());
            byte[] encryptData = HmacMD5.encryptHMAC(md.getBytes(), this.configSimulator.getServersignKey());
            String sign = HmacMD5.byteArrayToHexString(encryptData).toUpperCase();

            //封装成请求对象
            JSONObject hlhtParams = new JSONObject();
            hlhtParams.put("OperatorID", operator);
//            hlhtParams.put("PlatformID", operator);
            hlhtParams.put("Data", sendData);
            hlhtParams.put("TimeStamp", timeStamp);
            hlhtParams.put("Seq", seq);
            hlhtParams.put("Sig", sign);
            return hlhtParams;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取token
     *
     * @return
     * @throws Exception
     */
    public JSONObject queryToken() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("OperatorID", configSimulator.getClientoperatorId());
        serviceParams.put("OperatorSecret", configSimulator.getServersecretKey());

//        serviceParams.put("PlatformID", configSimulator.getClientoperatorId());
//        serviceParams.put("PlatformSecret", configSimulator.getServersecretKey());
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 查询站点
     *
     * @return
     * @throws Exception
     */
    public JSONObject queryStation() throws Exception {
        JSONObject serviceParams = new JSONObject();
//        serviceParams.put("OperatorID", null);
        serviceParams.put("StationIDs", null);
        serviceParams.put("LastQueryTime", null);
        serviceParams.put("PageNo", 1);
        serviceParams.put("PageSize", 50);

//        serviceParams.put("OperatorID", configSimulator.getClientoperatorId());
//        serviceParams.put("StationIDs",new String[]{"75","78"});
//        serviceParams.put("LastQueryTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date()));
//        serviceParams.put("PageNo", 1);
//        serviceParams.put("PageSize", 10);
        return this.packData(serviceParams.toJSONString());
    }

    public JSONObject startCharge() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("OrderNo", "202502040175003245222");
        //this.configSimulator.getClientcode() + "_" + this.configSimulator.getClientgun()
        serviceParams.put("ConnectorID", "10098700006_01");
        serviceParams.put("QRCode", "");
        return this.packData(serviceParams.toJSONString());
    }

    public JSONObject queryPriceRule() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("EquipBizSeq", this.configSimulator.getClientoperatorId() + "20210204017500324515");
        serviceParams.put("ConnectorID", this.configSimulator.getClientcode() + "_" + this.configSimulator.getClientgun());
//        serviceParams.put("QRCode", "");
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 查询订单信息
     *
     * @return
     */
    public JSONObject queryOrderStatus() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("OrderNo", "202502040175003245151");
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 停止充电
     *
     * @return
     * @throws Exception
     */
    public JSONObject stopCharge() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("OrderNo", "202502040175003245222");
        serviceParams.put("ConnectorID", "10098700006_01");
        return this.packData(serviceParams.toJSONString());
    }

    public JSONObject queryStationStatus() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("StationIDs", new String[]{"85"}); //this.configSimulator.getStationIDs()
//        serviceParams.put("ConnectorID", this.configSimulator.getClientcode()+"_"+this.configSimulator.getClientgun());
//        serviceParams.put("OperatorID", this.configSimulator.getClientoperatorId());
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 查询运营商信息
     *
     * @return
     */
    public JSONObject queryOperatorInfo() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("PageNo", 1);
        serviceParams.put("PageSize", 50);
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 设备认证
     *
     * @return
     * @throws Exception
     */
    public JSONObject queryEquipAuth() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("EquipAuthSeq", "000001");
        serviceParams.put("ConnectorID", "10098700006_1");
        return this.packData(serviceParams.toJSONString());
    }

    /**
     * 数据统计信息
     *
     * @return
     */
    public JSONObject queryStationStats() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("StationID",  "85");
        serviceParams.put("StartTime", "2025-01-01");
        serviceParams.put("EndTime", "2027-02-27");
        return this.packData(serviceParams.toJSONString());
    }

    public JSONObject queryFinishOrders() throws Exception {
        JSONObject serviceParams = new JSONObject();
        serviceParams.put("LastQueryTime", "2022-01-01 00:00:00");
        serviceParams.put("LastQueryEndTime", "2027-01-01 00:00:00");
        serviceParams.put("PageNo", 1);
        serviceParams.put("PageSize", 50);
        return this.packData(serviceParams.toJSONString());
    }
}
